/**
 * @file 初始化程序
 */
import { createWindow } from './hooks/window'
import { app, BrowserWindow, ipcMain } from 'electron'
import path from 'path'
import { initializeDb, getAllShortcuts, insertStudent, removeShortcut, readDb, writeDb, Shortcut } from './db.ts'
import { v4 as uuidv4 } from 'uuid'
app.whenReady().then(async () => {
  await initializeDb()
  createWindow()
  ipcMain.handle('insert-student', async (_event, studentData) => {
    const {  name, attempts, accuracy, points } = studentData
    await insertStudent(
      uuidv4(),
      name,
      attempts,
      accuracy,
      points
    )
    return { success: true }
  })

  ipcMain.handle('get-all-students', async () => {
    const students = await getAllShortcuts()
    return students
  })



  app.on('activate', () => {
    !BrowserWindow.getAllWindows().length && createWindow()
  })
})

app.on('window-all-closed', () => {
  process.platform !== 'darwin' && app.quit()
})

/**
 * 忽略警告
 */
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
